package com.toune.movie.mvp.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.toune.movie.R;
import com.toune.movie.base.AppConstant;
import com.toune.movie.base.BaseActivity;
import com.toune.movie.base.myUtil.GsonBinder;
import com.toune.movie.base.myUtil.MyUtil;
import com.toune.movie.mvp.adapter.AdapterButtonHor;
import com.toune.movie.mvp.adapter.AdapterButtonHor1;
import com.toune.movie.mvp.adapter.AdapterVarietyDetail;
import com.toune.movie.mvp.adapter.AdapterVarietySourDetail;
import com.toune.movie.mvp.contract.VarietyDetailContract;
import com.toune.movie.mvp.presenter.VarietyDetailPresenter;
import com.toune.movie.vo.VarietyDetailVo;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/5/15.
 */

public class VarietyDetailActivity extends BaseActivity<VarietyDetailPresenter> implements VarietyDetailContract.View {
    @BindView(R.id.rclview)
    RecyclerView rclview;
    @BindView(R.id.layout_ll)
    LinearLayout layoutLl;
    @BindView(R.id.img_iv)
    ImageView imgIv;
    @BindView(R.id.rclview_hor)
    RecyclerView rclviewHor;
    private AdapterVarietyDetail varietyDetailAdapter;
    private int tag;
    private String imgUrl;
    private AdapterVarietySourDetail adapterVarietySourDetail;

    @Override // 实例化presenter
    public VarietyDetailPresenter initPresenter() {
        return new VarietyDetailPresenter();
    }

    @Override
    public void loadMore() {

    }

    @Override
    public void refreData() {

    }

    private String url;

    @Override
    public void init(Bundle savedInstanceState) {
        titleStr = getIntent().getStringExtra("title");
        url = getIntent().getStringExtra("url");
        imgUrl = getIntent().getStringExtra("imgUrl");
        tag = getIntent().getIntExtra("tag", 0);
        Glide.with(context).load(imgUrl).into(imgIv);
        mPresenter.getData(url, tag);
        MyUtil.showProDialog(context);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_variety_detail;
    }

    @Override
    protected String getTitleStr() {
        return titleStr;
    }

    @Override
    protected void initEventAndData() {
    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void useNightMode(boolean isNight) {

    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (tag == 0) {
                AdapterButtonHor1 adapterButtonHor = new AdapterButtonHor1(R.layout.layout_radio_button, varietyDetailVo.getSourceLists());
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
                linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                rclviewHor.setLayoutManager(linearLayoutManager);
                rclviewHor.setAdapter(adapterButtonHor);
                adapterButtonHor.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        Intent intent = new Intent(context, WebActivity.class);
                        intent.putExtra("title", titleStr + "-" + varietyDetailVo.getSourceLists().get(position).getTitle());
                        intent.putExtra("url", varietyDetailVo.getSourceLists().get(position).getUrl());
                        startActivity(intent);
                    }
                });
            } else if (tag == 1) {
                final AdapterButtonHor1 adapterButtonHor = new AdapterButtonHor1(R.layout.layout_radio_button, varietyDetailVo.getSourceLists());
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
                linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                rclviewHor.setLayoutManager(linearLayoutManager);
                rclviewHor.setAdapter(adapterButtonHor);
                adapterButtonHor.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        adapterButtonHor.setSelectedPosition(position);
                        notifyAdapter(varietyDetailVo.getSourceLists().get(position));
                    }
                });
                notifyAdapter(varietyDetailVo.getSourceLists().get(0));
            } else if (tag == 2) {
                notifyVarietyAdapter(varietyDetailVo.getSourceLists());
            } else {
                notifyComicAdapter(varietyDetailVo.getSourceLists().get(0).getDataBeanList());
            }
            MyUtil.hidProDialog();
        }
    };

    private void notifyComicAdapter(final List<VarietyDetailVo.SourceList.DataBean> sourceLists) {
        if (varietyDetailAdapter == null) {
            varietyDetailAdapter = new AdapterVarietyDetail(R.layout.adapter_variety_detail, sourceLists);
            rclview.setLayoutManager(new GridLayoutManager(context, 3));
            rclview.setAdapter(varietyDetailAdapter);
            varietyDetailAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Intent intent = new Intent(context, WebActivity.class);
                    intent.putExtra("title", titleStr + "-" + sourceLists.get(position).getName());
                    intent.putExtra("url", AppConstant.JieXi + sourceLists.get(position).getUrl());
                    intent.putExtra("datas", GsonBinder.toJsonStr(sourceLists));
                    intent.putExtra("position", position);
                    intent.putExtra("tag", 3);
                    startActivity(intent);
                }
            });
        } else {
            varietyDetailAdapter.notifyDataSetChanged();
        }
    }

    private void notifyVarietyAdapter(final List<VarietyDetailVo.SourceList> sourceLists) {
        if (adapterVarietySourDetail == null) {
            adapterVarietySourDetail = new AdapterVarietySourDetail(R.layout.adapter_variety, sourceLists);
            rclview.setLayoutManager(new GridLayoutManager(context, 2));
            rclview.setAdapter(adapterVarietySourDetail);
            adapterVarietySourDetail.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Intent intent = new Intent(context, WebActivity.class);
                    intent.putExtra("title", titleStr + "-" + sourceLists.get(position).getTitle());
                    intent.putExtra("url", AppConstant.JieXi + sourceLists.get(position).getUrl());
//                    intent.putExtra("url", sourceLists.get(position).getUrl());
                    intent.putExtra("datas", GsonBinder.toJsonStr(sourceLists));
                    intent.putExtra("position", position);
                    intent.putExtra("tag", 2);
                    startActivity(intent);
                }
            });
        } else {
            adapterVarietySourDetail.notifyDataSetChanged();
        }
    }

    private VarietyDetailVo varietyDetailVo;

    @Override
    public void setData(final VarietyDetailVo vo) {
        varietyDetailVo = vo;
        handler.sendEmptyMessage(0);
    }

    private void notifyAdapter(final VarietyDetailVo.SourceList sourceList) {
        if (varietyDetailAdapter == null) {
            varietyDetailAdapter = new AdapterVarietyDetail(R.layout.adapter_variety_detail, sourceList.getDataBeanList());
            rclview.setLayoutManager(new GridLayoutManager(context, 3));
            rclview.setAdapter(varietyDetailAdapter);
            varietyDetailAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Intent intent = new Intent(context, WebActivity.class);
                    intent.putExtra("title", titleStr + "-" + sourceList.getDataBeanList().get(position).getName());
                    intent.putExtra("url", AppConstant.JieXi + sourceList.getDataBeanList().get(position).getUrl());
                    intent.putExtra("datas", GsonBinder.toJsonStr(sourceList.getDataBeanList()));
                    intent.putExtra("position", position);
                    intent.putExtra("tag", 1);
                    startActivity(intent);
                }
            });
        } else {
            varietyDetailAdapter.notifyDataSetChanged();
        }
    }
}
